<?php

use think\facade\Route;

return [
    'captchaRoute' => function () {
        Route::get('captcha/[:config]', "\\think\\captcha\\CaptchaController@index");
    },
    'staticRoute' => function () {
        Route::get('static/:path', function (string $path) {
            $filename = public_path() . 'static/' . $path;
            $mime_type = get_file_mime_type($filename);
            if ($mime_type) {
                return (new \think\swoole\response\File($filename))->contentType($mime_type);
            } else {
                return response()->code(404)->contentType('application/json');
            }
        })->pattern(['path' => '.*\.\w+$'])->allowCrossDomain();
    },
    'storageRoute' => function () {
        Route::get('storage/:path', function (string $path) {
            $filename = public_path() . 'storage/' . $path;
            $mime_type = get_file_mime_type($filename);
            if ($mime_type) {
                return (new \think\swoole\response\File($filename))->contentType($mime_type);
            } else {
                return response()->code(404)->contentType('application/json');
            }
        })->pattern(['path' => '.*\.\w+$']);
    },
    'payRoute' => function () {
        Route::group('pay', function () {
            // 唤起手机支付
            Route::rule('wapPay/[:id]', 'common.Pay/wapPay');
            // 支付回调
            Route::rule('payNotify/:pay_log_id', 'common.Pay/notify');
            // 支付返回
            Route::rule('payReturn/:pay_log_id', function () {
                return '<body><script>window.close();</script></body>';
            });
        });
    },
    'oauthRoute' => function () {
        // 绑定微信
        Route::rule('bindWechat/alarm/:id', 'common.Oauth/alarmBindWechat');
    },
    'qrcodeRoute' => function () {
        Route::group('qrcode', function () {
            Route::rule('get', function () {
                $request = request();
                $text = $request->param('text', '宜化智慧实验室');
                \QRcode::png($text, false, QR_ECLEVEL_H, 4, 3, false, 0xFFFFFF, 0x000000);
                $content = ob_get_clean();
                return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
            });
            Route::rule('download', function () {
                $request = request();
                $text = $request->param('text');
                \QRcode::png($text, false, QR_ECLEVEL_H, 4, 0, false, 0xFFFFFF, 0x000000);
                $content = ob_get_clean();
                return download($content, 'qrcode.png', true);
            });
        });
    },
    'entranceGuardRoute' => function () {
        Route::rule('entranceGuard/uploadFile/:items_id', 'common.EntranceGuard/uploadFile');
    }
];